﻿//--------------------------------------------------------------------------------------------------------------
// <copyright file="CSPCaseDetailWUC.cs" company="Microsoft Corporation" author="David Martos" date="01/01/2008">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//--------------------------------------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Publishing;
using Microsoft.Security.Application;

namespace CSP.WebParts.UserControls
{

    /// <summary>
    /// Shows the bottom menu. Gets the info from the Article Pages stored in root website.
    /// </summary>          
    public class CSPAuxiliaryMenuWUC : CSPBaseWUC 
    {       
        #region Members             
        protected Label lblCSPAuxiliaryMenu;        
        #endregion

        #region PageEvents
        /// <summary>
        /// Fired on page load
        /// </summary>  
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try
            {                       
                if (!Page.IsPostBack)
                {
                    this.LoadData();
                }                       
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPAuxiliaryMenuLoadDataErrorMessage), this.GetType().Name, ex);
            }
        }                
        #endregion


        #region Methods               
        /// <summary>
        /// Gets the Article Pages (name and url) stored in root website.
        /// </summary>          
        private void LoadData()
        {

            try
            {
                string result = string.Empty;

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
                    {
                        SPWeb contextWeb = site.RootWeb;
                        PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(contextWeb);

                        foreach (PublishingPage pp in pWeb.GetPublishingPages())
                        {

                            if (pp.Title != pWeb.DefaultPage.Title)
                                result += "<a href='" + AntiXss.HtmlAttributeEncode(pWeb.Url.ToString()) + "/" + AntiXss.HtmlAttributeEncode(pp.Url.ToString()) + "'>" + AntiXss.HtmlEncode(pp.Title.ToString()) + "</a>  |  ";
                        }

                        
                        if (result!=string.Empty)
                            result = result.Substring(0, result.Length - 5);
                    }
                });
                this.lblCSPAuxiliaryMenu.Text = result;
            }
            catch (CSP.Core.CSPException cspex)
            {                
                throw cspex;
            }
            catch (Exception ex)
            {
                this.LogMessage(this.LoadResource(WebPartConstants.CSPGenericErrorMessage), this.GetType().Name, ex);
                throw ex;
            }
        }              
        #endregion                   
    }
}
